#ifndef MESSAGESHOWAREA_H
#define MESSAGESHOWAREA_H

#include <QScrollArea>
#include <QWidget>
#include <QObject>
#include <QString>
#include <QEvent>
#include <QGridLayout>
#include <QPushButton>
#include <QMouseEvent>
#include <QLabel>
#include <QContextMenuEvent>
#include <QScrollBar>
#include <QPainter>
#include <QPainterPath>
#include <QPixmap>
#include <QPainter>
#include <QPainterPath>
#include <QFontMetrics>
#include <QByteArray>
#include "model/data.h"

using model::Message;

/* ----------------------- 消息展示区 ---------------------------- */
class MessageShowArea : public QScrollArea
{
    Q_OBJECT
public:
    explicit MessageShowArea(QWidget* parent = nullptr);

    void addMessage(bool isLeft, const Message& message);    // 默认尾插
    void addFrontMessage(bool isLeft, const Message& message);    // 头插
    void clear();        // 清空所有 Message
    void scrollToEnd(); // 设置滚动条滚动到末尾

protected:

private:
    QWidget* container;
};


/* ----------------------- 消息元素 ---------------------------- */
class MessageItem: public QWidget{
    Q_OBJECT

public:
    MessageItem(bool isLeft);

    // 工厂模式 来创建实例
    static MessageItem* makeMessageItem(bool isLeft, const Message& message);

private:
    bool isLeft;    // 判断是否是左侧消息

    // 工厂函数
    static QWidget* makeTextMessageItem(bool isLeft, const QString& text);
    static QWidget* makeImageMessageItem(bool isLeft, const QString& fileId, const QByteArray& content);
    static QWidget* makeFileMessageItem(bool isLeft, const Message& message);
    static QWidget* makeSpeechMessageItem(bool isLeft, const Message& message);
};

/* ----------------------- 文本消息正文部分 ---------------------------- */
class MessageContentLabel : public QWidget
{
    Q_OBJECT
public:
    MessageContentLabel(const QString &text, bool isLeft, model::MessageType messageType,
                        const QString& fileId, const QByteArray& content);

    QSize sizeHint() const override;
    QSize minimumSizeHint() const override;
    void paintEvent(QPaintEvent *event) override;

    void updateUI(const QString& fileId, const QByteArray& fileContent);
    void mousePressEvent(QMouseEvent* event) override;
    void saveAsFile(const QByteArray& content);

    // void contextMenuEvent(QContextMenuEvent* event) override;

private:
    QLabel* label; // 用于显示文本的 QLabel
    bool isLeft;   // 是否是左侧消息
    QString text; // 存储文本内容

    model::MessageType messageType;
    QString fileId;
    QByteArray content;
    bool loadContentDone = false;


    // 定义一些常量，保持气泡样式一致
    static constexpr int BUBBLE_RADIUS = 10;        // 气泡圆角半径
    static constexpr int ARROW_WIDTH = 10;          // 箭头宽度
    static constexpr int ARROW_HEIGHT = 10;         // 箭头高度 (三角形底边高)
    static constexpr int ARROW_Y_OFFSET = 15;       // 箭头从气泡顶部向下偏移的距离
    static constexpr int TEXT_HORIZONTAL_PADDING = 20; // 文本左右内边距总和 (例如，左右各10px)
    static constexpr int TEXT_VERTICAL_PADDING = 20;   // 文本上下内边距总和 (例如，上下各10px)
    static constexpr int MIN_BUBBLE_HEIGHT = 40;    // 气泡最小高度
    static constexpr int MAX_BUBBLE_WIDTH_RATIO = 60; // 气泡最大宽度占 MessageShowArea 宽度的百分比
};


/* ----------------------- 图片信息 ---------------------------- */
class MessageImageLabel : public QWidget{
    Q_OBJECT
public:
    MessageImageLabel(const QString& fileId, const QByteArray& content, bool isLeft);

    void updateUI(const QString& fileId, const QByteArray& content);
    void paintEvent(QPaintEvent* event);
    void resizeEvent(QResizeEvent *event);

    void setIconContent();
    QSize calculateScaledImageSize(const QByteArray& imageContent) const;
    QSize sizeHint() const;
    QSize minimumSizeHint() const;
private:
    QPushButton* imageBtn;
    QString fileId;     // 该图片对应文件 Id
    QByteArray content; // 图片二进制数据
    bool isLeft;
};

#endif // MESSAGESHOWAREA_H









